home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / HYP / C-D / DartmouthXCMDs.cpt / Dartmouth XCMDs Vol 1&2 / card_3697.txt < prev    next >
Text File  |  1989-02-26  |  10KB  |  351 lines

  1. -- card: 3697 from stack: in
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 3241
  5. -- name: 
  6.  
  7.  
  8. -- part 1 (button)
  9. -- low flags: 00
  10. -- high flags: A003
  11. -- rect: left=75 top=300 right=322 bottom=192
  12. -- title width / last selected line: 0
  13. -- icon id / first selected line: 0 / 0
  14. -- text alignment: 1
  15. -- font id: 0
  16. -- text size: 12
  17. -- style flags: 0
  18. -- line height: 16
  19. -- part name: ClipToPICT
  20. ----- HyperTalk script -----
  21. on mouseUp
  22.   put ClipToPICT(0,"Different Color")
  23. end mouseUp
  24.  
  25.  
  26.  
  27. -- part 2 (field)
  28. -- low flags: 80
  29. -- high flags: 2007
  30. -- rect: left=12 top=26 right=298 bottom=491
  31. -- title width / last selected line: 0
  32. -- icon id / first selected line: 0 / 0
  33. -- text alignment: 0
  34. -- font id: 22
  35. -- text size: 10
  36. -- style flags: 0
  37. -- line height: 13
  38. -- part name: Source
  39.  
  40.  
  41. -- part 3 (button)
  42. -- low flags: 00
  43. -- high flags: A003
  44. -- rect: left=314 top=300 right=322 bottom=435
  45. -- title width / last selected line: 0
  46. -- icon id / first selected line: 0 / 0
  47. -- text alignment: 1
  48. -- font id: 0
  49. -- text size: 12
  50. -- style flags: 0
  51. -- line height: 16
  52. -- part name: Show LSP Source
  53. ----- HyperTalk script -----
  54. on mouseUp
  55.   set the visible of card field 1 to not the visible of card field 1
  56.   if the visible of card field 1 is true then
  57.     set the name of me to "Hide LSP Source"
  58.   else set the name of me to "Show LSP Source"
  59. end mouseUp
  60.  
  61.  
  62.  
  63. -- part contents for background part 16
  64. ----- text -----
  65. CLIPTOPICT XFCN version 1.0
  66. Kevin Calhoun
  67.  
  68. ClipToPICT creates a PICT resource from a picture you've copied to the clipboard and copies it to the current stack.  You can tell ClipToPICT what ID number you want the PICT resource to have, or you can let it select an unused number for you.  If you choose a number that belongs to another PICT resource currently contained in your stack, the new picture will overwrite the old one.
  69.  
  70. You'll know when there's a picture on the clipboard by examing HyperCard's edit menu.  If the paste item says "Paste Picture," then there's a picture available for ClipToPICT to turn into a PICT resource.
  71.  
  72. INVOKING CLIPTOPICT
  73.  
  74. get ClipToPICT(pictID,pictName)
  75.  
  76. result:  resourceID
  77.  
  78. You must pass a number in the pictID parameter.  If you pass 0, ClipToPICT will find an unused ID number to assign the new PICT resource and, if it is copied successfully, return that ID.  If you pass any other number, ClipToPICT will use that number as the ID of the new PICT resource, replacing any old PICT resource in the current stack with the same ID, and, if it is copied successfully, return the same number.
  79.  
  80. You don't have to name the new PICT resource--the pictName parameter is optional.    If you specify a name that already belongs to a PICT resource in the current stack, ClipToPICT will replace the old resource with the new one.
  81.  
  82. If the clipboard contains no pictures, or if there was a problem accessing the scrap, opening the current stack's resource file, or writing the resource, Install Picture will return an error message.  Word 1 of this message will be "Error."
  83.  
  84. EXAMPLES
  85.  
  86. put ClipToPICT(0,"The Little Engine That Could") into pictNumber
  87. get ClipToPICT(2880,"")
  88.  
  89. REVISION HISTORY
  90. 1.0 -- 4/22/88
  91.  
  92. -- part contents for card part 2
  93. ----- text -----
  94. UNIT InstallPicture;
  95. { ClipToPICT XFCN ¬©1988 by the Trustees of Dartmouth College. }
  96. { Written by John K. Calhoun, Courseware Development. }
  97.  
  98. INTERFACE
  99.  USES
  100.   XCMDIntf, ROM85;
  101.  TYPE
  102.   Str31 = STRING[31];
  103.  
  104.  PROCEDURE Main (paramPtr : XCMDPtr);
  105.  
  106.  
  107. IMPLEMENTATION
  108.  
  109.  PROCEDURE DoJsr (addr : ProcPtr);
  110.  INLINE
  111.   $205F, $4E90;
  112.  
  113.  FUNCTION EvalExpr (paramPtr : XCmdPtr;
  114.          expr : Str255) : Handle;
  115.  BEGIN
  116.   WITH paramPtr^ DO
  117.    BEGIN
  118.     inArgs[1] := ORD(@expr);
  119.     request := xreqEvalExpr;
  120.     DoJsr(entryPoint);
  121.     EvalExpr := Handle(outArgs[1]);
  122.    END;
  123.  END;
  124.  
  125.  FUNCTION StringLength (paramPtr : XCmdPtr;
  126.          strPtr : Ptr) : LongInt;
  127.  BEGIN
  128.   WITH paramPtr^ DO
  129.    BEGIN
  130.     inArgs[1] := ORD(strPtr);
  131.     request := xreqStringLength;
  132.     DoJsr(entryPoint);
  133.     StringLength := outArgs[1];
  134.    END;
  135.  END;
  136.  
  137.  FUNCTION NumToStr (paramPtr : XCmdPtr;
  138.          num : LongInt) : Str31;
  139.   VAR
  140.    str : Str31;
  141.  BEGIN
  142.   WITH paramPtr^ DO
  143.    BEGIN
  144.     inArgs[1] := num;
  145.     inArgs[2] := ORD(@str);
  146.     request := xreqNumToStr;
  147.     DoJsr(entryPoint);
  148.     NumToStr := str;
  149.    END;
  150.  END;
  151.  
  152.  PROCEDURE ZeroToPas (paramPtr : XCmdPtr;
  153.          zeroStr : Ptr;
  154.          VAR pasStr : Str255);
  155.  BEGIN
  156.   WITH paramPtr^ DO
  157.    BEGIN
  158.     inArgs[1] := ORD(zeroStr);
  159.     inArgs[2] := ORD(@pasStr);
  160.     request := xreqZeroToPas;
  161.     DoJsr(entryPoint);
  162.    END;
  163.  END;
  164.  
  165.  FUNCTION PasToZero (paramPtr : XCmdPtr;
  166.          str : Str255) : Handle;
  167.  BEGIN
  168.   WITH paramPtr^ DO
  169.    BEGIN
  170.     inArgs[1] := ORD(@str);
  171.     request := xreqPasToZero;
  172.     DoJsr(entryPoint);
  173.     PasToZero := Handle(outArgs[1]);
  174.    END;
  175.  END;
  176.  
  177.  FUNCTION StrToNum (paramPtr : XCmdPtr;
  178.          str : Str31) : LongInt;
  179.  BEGIN
  180.   WITH paramPtr^ DO
  181.    BEGIN
  182.     inArgs[1] := ORD(@str);
  183.     request := xreqStrToNum;
  184.     DoJsr(entryPoint);
  185.     StrToNum := outArgs[1];
  186.    END;
  187.  END;
  188.  
  189.  FUNCTION GetTheNameOfThisStack (paramPtr : XCMDPtr) : Str255;
  190.   VAR
  191.    str1, str2 : str255;
  192.    theResult : Handle;
  193.    theLength : INTEGER;
  194.  BEGIN
  195.   str1 := 'word 2 of the long name of this stack';
  196.   theResult := EvalExpr(paramPtr, str1);
  197.   IF theResult <> NIL THEN
  198.    BEGIN
  199.     theLength := StringLength(paramPtr, theResult^);
  200.     DisposHandle(theResult);
  201.  
  202.     str2 := NumToStr(paramPtr, theLength - 1);
  203.     theResult := EvalExpr(paramPtr, CONCAT('char 2 to ', str2, ' of ', str1));
  204.     IF theResult <> NIL THEN
  205.      BEGIN
  206.       ZeroToPas(paramPtr, theResult^, str1);
  207.       DisposHandle(theResult);
  208.      END
  209.     ELSE
  210.      str1 := '';
  211.    END
  212.   ELSE
  213.    str1 := '';
  214.   GetTheNameOfThisStack := str1;
  215.  END;
  216.  
  217.  PROCEDURE GetPictScrap (paramPtr : XCMDPtr);
  218.   VAR
  219.    parameterCount : INTEGER;
  220.    id : INTEGER;
  221.    name : Str255;
  222.    scrapLength : longint;
  223.    offset : longint;
  224.    thePicHandle : Handle;
  225.    str : Str255;
  226.    myStack : INTEGER;
  227.    resAlready : Handle;
  228.  
  229.   PROCEDURE passReturnValue (theMsg : Str255); { set theResult and quit }
  230.   BEGIN
  231.    paramPtr^.returnValue := PasToZero(paramPtr, theMsg);
  232.   END;
  233.  
  234.   PROCEDURE GetParameters;
  235.   BEGIN
  236.    parameterCount := paramPtr^.paramCount;
  237.    IF parameterCount > 0 THEN
  238.     BEGIN
  239.      ZeroToPas(paramPtr, paramPtr^.params[1]^, str);
  240.      id := StrToNum(paramPtr, str);
  241.      IF parameterCount > 1 THEN
  242.       ZeroToPas(paramPtr, paramPtr^.params[2]^, name)
  243.      ELSE
  244.       name := '';
  245.     END;
  246.   END;
  247.  
  248.   PROCEDURE CheckForSameTypeIDName;
  249. { Remove all PICT resources of the same ID and the same name as }
  250. { the one we're going to copy. Why?  Because... }
  251. { "When you add a resource to a file, the Resource Manager doesn't check }
  252. { to see if the file already has a resource with the type and ID you're }
  253. { trying to add...  ...future GetResource calls will never return the new }
  254. { resource that you just added, since the one that was there previously }
  255. { comes before the new one in the resource file's map...  This means that }
  256. { the new resource you just added is impossible to access." }
  257. {    --Scott Knaster, How To Write Macintosh Software, p. 329 }
  258. {      Hayden Book Company, Hasbrouck Heights, NJ, 1986. }
  259.   BEGIN
  260.    IF id = 0 THEN
  261.     REPEAT
  262.      id := Unique1ID('PICT');
  263.     UNTIL id > 127
  264.    ELSE
  265.     BEGIN
  266.      REPEAT
  267.       resAlready := Get1Resource('PICT', id);
  268.       IF resAlready <> NIL THEN
  269.        BEGIN
  270.         RmveResource(resAlready);
  271.         DisposHandle(resAlready);
  272.        END;
  273.      UNTIL resAlready = NIL;
  274.  
  275.      IF name <> '' THEN
  276.       BEGIN
  277.        REPEAT
  278.         resAlready := Get1NamedResource('PICT', name);
  279.         IF resAlready <> NIL THEN
  280.          BEGIN
  281.           RmveResource(resAlready);
  282.           DisposHandle(resAlready);
  283.          END;
  284.        UNTIL resAlready = NIL;
  285.       END;
  286.     END;
  287.   END;
  288.  
  289.  BEGIN
  290.   GetParameters;
  291.   IF parameterCount > 0 THEN
  292.    BEGIN
  293.     str := GetTheNameOfThisStack(paramPtr);
  294.     IF str <> '' THEN
  295.      BEGIN
  296.       myStack := OpenResFile(str);
  297.       IF (myStack = -1) AND (ResError = eofErr) THEN
  298.        BEGIN
  299.         CreateResFile(str);
  300.         IF ResError = noErr THEN
  301.          myStack := OpenResFile(str);
  302.        END;
  303.       IF myStack > -1 THEN
  304.        BEGIN
  305.         scrapLength := GetScrap(NIL, 'PICT', offset);
  306.         IF scrapLength >= 0 THEN
  307.          BEGIN
  308.           thePicHandle := NewHandle(0);
  309.           IF thePicHandle <> NIL THEN
  310.            BEGIN
  311.             scrapLength := GetScrap(thePicHandle, 'PICT', offset);
  312.             IF scrapLength > 0 THEN
  313.              BEGIN
  314.               HNoPurge(Handle(thePicHandle));
  315.               UseResFile(myStack);
  316.               CheckForSameTypeIDName;
  317.               AddResource(thePicHandle, 'PICT', id, name);
  318.               IF ResError = noErr THEN
  319.                BEGIN
  320.                 SetResAttrs(thePicHandle, resPurgeable + resChanged);
  321.                 WriteResource(thePicHandle);
  322.                END;
  323.               UpdateResFile(myStack);
  324.               HPurge(Handle(thePicHandle));
  325.               passReturnValue(NumToStr(paramPtr, id));
  326.              END  { if we read the scrap successfully }
  327.             ELSE
  328.              passReturnValue(CONCAT('Error ', NumToStr(paramPtr, scrapLength)));
  329.            END  { if thePicHandle <> nil }
  330.           ELSE
  331.            passReturnValue(CONCAT('Error ', NumToStr(paramPtr, MemError)));
  332.          END  { if there is scrap of type PICT }
  333.         ELSE
  334.          passReturnValue(CONCAT('Error ', NumToStr(paramPtr, scrapLength), ' (no pictures on clipboard)'));
  335.        END  { if we opened resource fork successfully }
  336.       ELSE
  337.        passReturnValue(CONCAT('Error ', NumToStr(paramPtr, ResError)));
  338.      END
  339.     ELSE
  340.      passReturnValue('Error (can''t get stack name)');
  341.    END
  342.   ELSE
  343.    passReturnValue('ClipToPICT XCMD 1.0 -- April 22, 1988');
  344.  END;
  345.  
  346.  PROCEDURE Main;
  347.  BEGIN
  348.   GetPictScrap(paramPtr);
  349.  END;
  350.  
  351. END.